﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq;

/// <summary>
///Client 的摘要说明
/// </summary>
public partial class Client
{

    public enum type { 普通用户 = 1, 个人老师, 机构 }
    public enum userstatus { 正常 = 1, 冻结 = 2, 未激活 = 3 }
    public enum sex { 暂无资料 = 0, 男 = 1, 女 }
    public enum safeLevel { 低级 = 1, 中级, 高级 }
    public static Client Get(int id)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            return context.Client.FirstOrDefault(row => row.ID == id && row.Enable_Flag == 1);
        }
    }

    public static List<Client> GetAll()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            return context.Client.Where(row => row.Enable_Flag == 1).ToList();
        }
    }

    public static Client GetSerchByUserName(string UserName)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            return context.Client.Where(row => row.Username == UserName || UserName.IndexOf(row.Username) > -1 && row.Enable_Flag == 1).ToList().FirstOrDefault();
        }
    }

    public static Client GetByUserName(string UserName)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            return context.Client.FirstOrDefault(row => row.Username == UserName && row.Enable_Flag == 1);
        }
    }

    public static Client GetByEmail(string Email)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            return context.Client.FirstOrDefault(row => row.Email == Email && row.Enable_Flag == 1);
        }
    }


    public static Client GetByPhone(string Phone)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            return context.Client.FirstOrDefault(row => row.Telephone == Phone && row.Enable_Flag == 1);
        }
    }


    public static Client GetByUserNameOrEmailOrPhone_Password(string UserName, string Pass)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            return context.Client.FirstOrDefault(row => (row.Username == UserName || row.Email == UserName || row.Telephone == UserName) && row.Password == Pass && row.Enable_Flag == 1);
        }
    }

    public void Add()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            context.Client.InsertOnSubmit(this);
            context.SubmitChanges();
        }
    }

    public void Update()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            this.GetType().GetMethod("Detach").Invoke(this, null);
            var tbl = context.GetTable<Client>();
            tbl.Attach(this);
            context.Refresh(RefreshMode.KeepCurrentValues, this);
            context.SubmitChanges();
        }
    }

    public static void Delete(int id)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            var objItem = context.Client.FirstOrDefault(row => row.ID == id);
            if (objItem == null)
                return;
            context.SubmitChanges();
        }
    }

    public void SetCurrent()
    {
        HttpContext.Current.Session["Client"] = this.ID;
    }

    public static Client GetCurrent()
    {
        if (HttpContext.Current.Session["Client"] == null)
            return null;
        else
            return Client.Get(DongDong.Common.Misc.ConvertToInt(HttpContext.Current.Session["Client"]));
    }

    public static void ClearCurrent()
    {
        HttpContext.Current.Session["Client"] = null;
    }

    public static List<Client> GetAllByCondition(int shengID, int shiID, int quID, int Sex, int UserType, string Tel, string Mail, string JiFenFrom, string JiFento, string TimeFrom, string TimeTo, string Key, int UserStatus)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            var query = from user in context.Client
                        where user.Enable_Flag == 1
                        select user;
            if (shengID != 0)
            {
                query = query.Where(row => row.ProvinceID == shengID);
            }
            if (shiID != 0)
            {
                query = query.Where(row => row.CityID == shiID);
            }
            if (quID != 0)
            {
                query = query.Where(row => row.ReginID == quID);
            }
            if (Sex != 0)
            {
                query = query.Where(row => row.Sex == Sex);
            }
            if (UserType != 0)
            {
                query = query.Where(row => row.Type == UserType);
            }

            if (!string.IsNullOrEmpty(Tel))
            {
                query = query.Where(row => row.Telephone.Contains(Tel));
            }

            if (!string.IsNullOrEmpty(Mail))
            {
                query = query.Where(row => row.Email.Contains(Mail));
            }

            //if (!string.IsNullOrEmpty(JiFenFrom))
            //{
            //    query = query.Where(row => row.AddTime >= DongDong.Common.Misc.ConvertToDateTime(TimeFrom));
            //}
            if (!string.IsNullOrEmpty(TimeTo))
            {
                query = query.Where(row => row.AddTime <= DongDong.Common.Misc.ConvertToDateTime(TimeTo));
            }


            if (!string.IsNullOrEmpty(TimeFrom))
            {
                query = query.Where(row => row.AddTime >= DongDong.Common.Misc.ConvertToDateTime(TimeFrom));
            }
            if (!string.IsNullOrEmpty(TimeTo))
            {
                query = query.Where(row => row.AddTime <= DongDong.Common.Misc.ConvertToDateTime(TimeTo));
            }


            if (!string.IsNullOrEmpty(Key))
            {
                query = query.Where(row => row.Username.Contains(Key) || row.NiCheng.Contains(Key) || row.RealName.Contains(Key));
            }

            if (UserStatus != 0)
            {
                query = query.Where(row => row.Status == UserStatus);
            }
            return query.OrderByDescending(row => row.AddTime).ToList();
        }
    }

}
